package cn.tramy.pinxian.wind.eventstore.storage

import cn.tramy.pinxian.common.util.logger
import cn.tramy.pinxian.wind.eventstore.query.entry.DomainEventEntry
import cn.tramy.pinxian.wind.eventstore.query.entry.DomainEventEntryRepository
import reactor.core.publisher.Flux

class JpaEventStorage(private val domainEventEntryRepository: DomainEventEntryRepository) : EventStorage {


    companion object {
        val LOGGER = logger(JpaEventStorage::class.java)
    }

    override fun persistent(eventEntry: DomainEventEntry) {
        LOGGER.debug("persist event: $eventEntry")
        this.domainEventEntryRepository.save(eventEntry)

    }

    override fun readEventEntry(aggregateId: String, aggregateType: String): Flux<DomainEventEntry> {
        LOGGER.debug("read events of aggregate [$aggregateType:$aggregateId]")
        return this.domainEventEntryRepository.findByAggregateIdAndAggregateType(aggregateId, aggregateType)
    }
}